; game.asm
; game play routines

	.include "global.inc"
	.include "sprites.inc"
	.include "colors.inc"
	.include "sound.inc"
	.include "frame.inc"
	.include "eightplay.inc"
	_IN_GAME = 1
	.include "game.inc"
	
	MAX_PLAYERS = 9
	FADE_FRAMES = 3

	FIRE_UP = 0
	FIRE_UP_RIGHT = 1
	FIRE_RIGHT = 2
	FIRE_DOWN_RIGHT = 3
	FIRE_DOWN = 4
	FIRE_DOWN_LEFT = 5
	FIRE_LEFT = 6
	FIRE_UP_LEFT = 7
	
	PLAYER_WAIT_RESTART = 20
	
.macro getjoypos
			lda joypos, x
.endmacro	
	
	.segment "JMPTBL"
	
joyptr:		.word 0
	
	.segment "CODE"
	
initgame:	ldx #MAX_PLAYERS-1
			lda #0
			sta plrcnt
			sta setcnt			
@iplrlp:	sta plractive, x
			sta plrlife, x
			sta plrwait, x
			dex
			bpl @iplrlp
			lda #0
			sta animbit
			sta animtgl
			jmp clrlfbar

initplr:	; x ... player index
			lda #1
			sta plractive, x
			sta spr2x2act, x
			lda plrcolumn, x
			sta spr2x2col, x
			lda plrrow, x
			sta spr2x2row, x
			lda plrpix, x
			sta spr2x2pix, x
			lda plrpiy, x
			sta spr2x2piy, x
			lda plrshape, x
			sta spr2x2idx, x
			lda plrcolor, x
			sta spr2x2clr, x
			lda #0
			sta plrhit, x
			rts
			
initlife:	; init life indicator
			lda #9
			sta plrlife, x
			ldy #10
			lda plrcolor, x
			jmp showlife

execgame:	lda animtgl
			eor #1
			sta animtgl
			eor animbit
			sta animbit
			lda #15-MAX_PLAYERS
			sta sndvol
			ldx #MAX_PLAYERS-1
@plrlp:		jsr execshts
			jsr execplyr
			dex
			bpl @plrlp
			lda sndvol
			jsr setvol
			rts

setgame:	lda animtgl
			eor #1
			sta animtgl
			eor animbit
			sta animbit
			ldx #MAX_PLAYERS-1
@plrlp:		jsr execset
			dex
			bpl @plrlp
			rts

setdone:	lda setcnt
			beq @chkplr 
			lda #0
			beq @done
@chkplr:	lda plrcnt
@done:		rts			

startgame:	ldx #MAX_PLAYERS-1
			stx actplr
@plrlp:		ldx actplr
			lda plractive, x
			beq @next
			lda #SHAPE_IDLE
			sta spr2x2idx, x
			jsr updlife 
@next:		dec actplr
			bpl @plrlp
			lda #15
			jsr setvol
			ldy #SOUND_GO
			jmp sound1

updlife:	lda plrlife, x
			tay
			lda plrcolor, x
			jmp showlife

execshts:	; handle bullet x
			lda spr1x1act, x
			beq @done
			lda spr1x1coll, x
			beq @next
			lda #0
			sta spr1x1act, x
			beq @done  
@next:		lda shtdir, x
			tay
			lda shots_lo, y
			sta joyptr
			lda shots_hi, y
			sta joyptr+1
			jmp (joyptr)
@done:		rts

gameover:	; check for winner
			lda plrcnt
			cmp #1
			bne @notover
			ldy #MAX_PLAYERS-1
@find:		lda plractive, y
			bne @found
@next:		dey
			bpl @find
@found:		lda plrhit, y
			bne @next
			lda #0
@notover:	rts

execplyr:	; handle player x
			lda plractive, x	; player active?
			sta spr2x2act, x
			beq @chklife		; no -> check restart
			lda plrhit, x
			beq @chkcol
			jmp dohit
@chkcol:	lda spr2x2coll, x	; collision ?
			beq @goon
			jmp docoll			; yes -> handle it
@goon:		getjoypos
			tay
			lda actions_lo, y
			sta joyptr
			lda actions_hi, y
			sta joyptr+1
			jmp (joyptr)
@chklife:	lda plrlife, x
			beq @done
@chkwait:	lda plrwait, x
			beq @restore
			dec plrwait, x
			bne @done
@restore:	jsr initplr
			jmp sndthud
@done:		rts

	.macro still_shape shape
			lda #shape
			sta spr2x2idx, x
	.endmacro 

	.macro mving_shape shape
			jsr sndsteps
			lda #shape
			ora animbit
			sta spr2x2idx, x
	.endmacro 

execset:	lda plractive, x	; player active?
			sta spr2x2act, x
			beq @chksel			; no -> check action
			lda plrrdy, x		; player ready ?
			beq @chkrdy			; no -> check
			lda #SHAPE_UPDN1
			ora animbit
			sta spr2x2idx, x
			jmp @done
@chkrdy:	lda plrcnt			; 2 players min
			cmp #2
			bcc @done
			lda plrwait, x
			beq @chkbutton
			dec plrwait, x
			bne @done
@chkbutton:	getjoypos
			and #JOY_FIRE
			bne @chkcolor
			dec setcnt
			lda #1
			sta plrrdy, x
			jmp sndblip
@chkcolor:	getjoypos
			and #JOY_RIGHT			; right 
			beq	@clrrght
			getjoypos
			and #JOY_LEFT			; left 
			beq @clrleft
			bne @done
@chksel:	getjoypos
			and #JOY_FIRE
			bne @done
			jsr initplr
			jsr initlife
			inc plrcnt
			inc setcnt
			lda #0
			sta plrrdy, x
			lda #12
			sta plrwait, x
			lda #SHAPE_IDLE
			sta spr2x2idx, x
			jmp sndthud
@clrleft:	
@clrrght:	ldy plrcolor, x
			bne @inc1
			iny
@inc1:		iny
			jmp setplrclr
@done:		rts

setplrclr:	tya
			and #7
			sta plrcolor, x
			sta spr2x2clr, x
			lda #12
			sta plrwait, x
			rts

sndsteps:	inc sndvol
			ldy #SOUND_STEPS
			jmp sound2

sndthud:	inc sndvol
			ldy #SOUND_THUD
			jmp sound1

sndblip:	inc sndvol
			ldy #SOUND_BLIP
			jmp sound3

sndnoise:	inc sndvol
			ldy #SOUND_NOISE
			jmp sound4
			
	.macro shiftup
		.local @done			
			dec spr2x2piy, x
			bpl @done
			lda #7
			sta spr2x2piy, x
			dec spr2x2row, x
	@done:
	.endmacro
	
	.macro shiftdn	
		.local @done			
			inc spr2x2piy, x
			lda spr2x2piy, x
			cmp #8
			bcc @done
			lda #0
			sta spr2x2piy, x
			inc spr2x2row, x
	@done:
	.endmacro
		
	.macro shiftleft	
		.local @done			
			dec spr2x2pix, x
			bpl @done
			lda #7
			sta spr2x2pix, x
			dec spr2x2col, x
	@done:
	.endmacro
	
	.macro shiftright	
		.local @done			
			inc spr2x2pix, x
			lda spr2x2pix, x
			cmp #8
			bcc @done
			lda #0
			sta spr2x2pix, x
			inc spr2x2col, x
	@done:
	.endmacro

docoll:		; handle collision
			inc plrhit, x
			lda plrlife, x
			bne @declife
			ldy #10
			lda plrcolor, x
			jsr showlife
			jmp @done
@declife:	dec plrlife, x
			bne @uplive
			dec plrcnt
			lda plrcnt
			cmp #2
			bcs @uplive
			ldy #SOUND_OVER
			jsr sound1
@uplive:	jsr updlife
@done:		still_shape SHAPE_DYING1
			jmp sndnoise

dohit:		inc plrhit, x
			lda plrhit, x
			cmp #(2*FADE_FRAMES)
			bcc @fade1
			lda #0
			sta plrhit, x
			sta plractive, x
			lda #PLAYER_WAIT_RESTART
			sta plrwait, x
			rts
@fade1:		cmp #FADE_FRAMES
			bcc @done
			still_shape SHAPE_DYING2
@done:		rts			

nomove:		still_shape SHAPE_IDLE
			rts

moveup:		mving_shape SHAPE_UPDN1
			shiftup
			rts

mvuprght:	mving_shape SHAPE_RIGHT1
			shiftup
			shiftright
			rts

mvupleft:	mving_shape SHAPE_LEFT1
			shiftup
			shiftleft
			rts
			
movedn:		mving_shape SHAPE_UPDN1
			shiftdn
			rts

mvdnleft:	mving_shape SHAPE_LEFT1
			shiftdn
			shiftleft
			rts

mvdnrght:	mving_shape SHAPE_RIGHT1
			shiftdn
			shiftright
			rts

moveleft:	mving_shape SHAPE_LEFT1
			shiftleft
			rts

moverght:	mving_shape SHAPE_RIGHT1
			shiftright
			rts

fireup:		still_shape SHAPE_FIRE
			ldy #FIRE_UP
			bpl dofire

frupleft:	still_shape SHAPE_FIRE
			ldy #FIRE_UP_LEFT
			bpl dofire

fruprght:	still_shape SHAPE_FIRE
			ldy #FIRE_UP_RIGHT
			bpl dofire
			rts
			
firedn:		still_shape SHAPE_FIRE
			ldy #FIRE_DOWN
			bpl dofire
			rts

frdnleft:	still_shape SHAPE_FIRE
			ldy #FIRE_DOWN_LEFT
			bpl dofire

frdnrght:	still_shape SHAPE_FIRE
			ldy #FIRE_DOWN_RIGHT
			bpl dofire
			
fireleft:	still_shape SHAPE_FIRE
			ldy #FIRE_LEFT
			bpl dofire
			
firerght:	still_shape SHAPE_FIRE	
			ldy #FIRE_RIGHT
			; fall through
dofire:		lda spr1x1act, x
			bne @done					; option?: guided missile move these lines 2 down 
			tya
			sta shtdir, x
			lda spr2x2col, x
			sta spr1x1col, x
			clc
			lda spr2x2pix, x
			adc shots_dx, y
			bmi @negx
@dxloop:	cmp #8
			bcc @storex
			sbc #8
			inc spr1x1col, x
			bne @dxloop
@negx:		clc
			adc #7
			dec spr1x1col, x
@storex:	sta spr1x1pix, x
			lda spr2x2row, x
			sta spr1x1row, x
			clc
			lda spr2x2piy, x
			adc shots_dy, y
			bmi @negy
@dyloop:	cmp #8
			bcc @storey
			sbc #8
			inc spr1x1row, x
			bne @dyloop
@negy:		clc
			adc #7
			dec spr1x1row, x
@storey:	sta spr1x1piy, x
			jsr sndblip
@done:		lda #1
			sta spr1x1act, x
			lda spr2x2clr, x
			sta spr1x1clr, x
			rts

shotup:		dec spr1x1piy, x
			bpl @done
			dec spr1x1row, x
			lda #7
			sta spr1x1piy, x
@done:		rts

shuprhgt:	jsr shrght	
			jmp shotup

shrght:		lda spr1x1pix, x
			cmp #7
			bcc @novfl
			lda #$FF
			sta spr1x1pix, x
			inc spr1x1col, x
@novfl:		inc spr1x1pix, x
			rts

shdnrght:	jsr shrght	
			jmp shotdn

shotdn:		lda spr1x1piy, x
			cmp #7
			bcc @novfl
			lda #$FF
			sta spr1x1piy, x
			inc spr1x1row, x
@novfl:		inc spr1x1piy, x
			rts

shdnleft:	jsr shleft
			jmp shotdn

shleft:		dec spr1x1pix, x
			bpl @done
			dec spr1x1col, x
			lda #7
			sta spr1x1pix, x
@done:		rts

shupleft:	jsr shleft
			jmp shotup

	.segment "RODATA"

actions_lo:	.byte <nomove, <nomove, <nomove, <nomove, <nomove, <frdnrght, <fruprght, <firerght
			.byte <nomove, <frdnleft, <frupleft, <fireleft, <nomove, <firedn, <fireup, <nomove
			.byte <nomove, <nomove, <nomove, <nomove, <nomove, <mvdnrght, <mvuprght, <moverght
			.byte <nomove, <mvdnleft, <mvupleft, <moveleft, <nomove, <movedn, <moveup, <nomove
actions_hi: .byte >nomove, >nomove, >nomove, >nomove, >nomove, >frdnrght, >fruprght, >firerght
			.byte >nomove, >frdnleft, >frupleft, >fireleft, >nomove, >firedn, >fireup, >nomove
			.byte >nomove, >nomove, >nomove, >nomove, >nomove, >mvdnrght, >mvuprght, >moverght
			.byte >nomove, >mvdnleft, >mvupleft, >moveleft, >nomove, >movedn, >moveup, >nomove	

shots_lo:	.byte <shotup, <shuprhgt, <shrght, <shdnrght, <shotdn, <shdnleft, <shleft, <shupleft 
shots_hi:	.byte >shotup, >shuprhgt, >shrght, >shdnrght, >shotdn, >shdnleft, >shleft, >shupleft
shots_dx:	.byte $04, $08, $09, $09, $04, $FF, $FF, $00
shots_dy:   .byte $FF, $FF, $03, $08, $09, $08, $03, $FF

plrcolumn:	.repeat 3, I
				.repeat 3, J
					.byte (COLS*(1+J)-2)/4					
				.endrep
			.endrep 
plrrow:		.repeat 3, I
				.repeat 3, J
					.byte (ROWS*(1+I)-2)/4					
				.endrep
			.endrep
plrpix:		.repeat 3, I
				.repeat 3, J
					.byte (2*COLS*(1+J)-4)&7					
				.endrep
			.endrep
plrpiy:		.repeat 3, I
				.repeat 3, J
					.byte (2*ROWS*(1+I)-4)&7					
				.endrep
			.endrep
plrshape:	.repeat MAX_PLAYERS, I
				.byte SHAPE_IDLE
			.endrep
plrcolor:	.repeat MAX_PLAYERS, I
				.byte BLACK
			.endrep

	.segment "BSS"
	
plractive:	.res MAX_PLAYERS
plrwait:	.res MAX_PLAYERS
plrhit:		.res MAX_PLAYERS
plrrdy:		.res MAX_PLAYERS
plrlife:	.res MAX_PLAYERS
animbit:	.res 1
animtgl:	.res 1
sndvol:		.res 1
plrcnt:		.res 1
setcnt:		.res 1
actplr:		.res 1

shtdir:		.res MAX_PLAYERS
